from django.shortcuts import render,HttpResponse
from  .models import  Course,Category
from  user.models import  User


# Create your views here.
def huiqu(request):
    user_id = ''
    user_account = ''
    try:
        session_user = request.session['session_user']
        user_id = session_user.get('id')
        user_account = session_user.get('account')
    except:
        user_id ='0'
        user_account = '0'

    ret= {'user_id': user_id, "user_account": user_account}
    return ret

# Create your views here.
def index(request,course_id):
    info = Course.objects.get(id = course_id)
    goumai = 0
    session_user = huiqu(request)
    if session_user['user_id'] != "0":
        user = User.objects.get( id = session_user['user_id'])
        try:
            jg = info.buyer.get(id = user.id)
        # print(jg)
            if jg:
                goumai = 1
        except:
            pass
    context = {"info":info,"goumai":goumai}
    return render(request,'info.html',context)